package giit.computerbase.action;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import giit.computerbase.server.ActivityInformationServer;
import giit.computerbase.server.HistoryServer;
import giit.computerbase.util.Application;
import giit.computerbase.util.DealWithAddress;
import giit.computerbase.util.DealWithCharcter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2018-11-28 Wednesday 10:28:40
 */
public class EditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));
        response.setCharacterEncoding((String) Application.getConfig().pageConfig.get("encode"));

        String user = (String) request.getSession().getAttribute("user");
        if (user == null || user.equals("")) {
            response.sendRedirect("index");
        }
        SmartUpload smartUpload = new SmartUpload();
        smartUpload.initialize(getServletConfig(), request, response);
        try {
            smartUpload.upload();
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        Request r = smartUpload.getRequest();
        String title = r.getParameter("title");
        String main = r.getParameter("main");
        String contributor = r.getParameter("contributor");
        String activityTypeID = r.getParameter("type");
        String feedTime = r.getParameter("feedTime").replace("-", "/");
        String linkContent = r.getParameter("link_content");

        HistoryServer historyServer = new HistoryServer();
        String ua = request.getHeader("User-Agent");
        boolean flag = false;
        String toPath = "warn.jsp?";
        if (title != null && !title.equals("") && main != null && !main.equals("") && contributor != null && !contributor.equals("")
                && activityTypeID != null && !activityTypeID.equals("") && feedTime != null && !feedTime.equals("")) {
            title = URLDecoder.decode(title, "utf-8");
            main = URLDecoder.decode(main, "utf-8");
            contributor = URLDecoder.decode(contributor, "utf-8");
            activityTypeID = URLDecoder.decode(activityTypeID, "utf-8");
            linkContent = URLDecoder.decode(linkContent, "utf-8");
            String activityID = Application.getUUID();
            Date date = new Date();
            Date feedDate = new Date(feedTime);
            ActivityInformationServer activityInformationServer = new ActivityInformationServer();

            title = DealWithCharcter.dealWith(title);
            main = DealWithCharcter.dealWith(main);
            contributor = DealWithCharcter.dealWith(contributor);
            activityTypeID = DealWithCharcter.dealWith(activityTypeID);
            linkContent = DealWithCharcter.dealWith(linkContent);
            if(linkContent!=null&&!linkContent.equals("")){
                main += ("<br><br><br>官网新闻链接：<a href=\"" + linkContent + "\" target=\"_blank\">" + title + "</a>");
            }

            flag = activityInformationServer.addActivityInformation(activityID, title, main, contributor, feedDate, activityTypeID, date);
            if (flag) {
                int count = smartUpload.getFiles().getCount();
                if (count != 0) {
                    try {
                        String filename = Application.PATH + File.separator + "upload" + File.separator + title + File.separator;
                        File file = new File(filename);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        String filesName;
                        for (int i = 0; i < count; i++) {
                            filesName = filename + smartUpload.getFiles().getFile(i).getFileName();
                            smartUpload.getFiles().getFile(i).saveAs(filesName);
                        }
                    } catch (SmartUploadException e) {
                    }
                }
                toPath += ("state=panel-primary&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("发布成功，点击返回按钮返回主页面") + "&preview=home.jsp");
            } else {
                toPath += ("state=panel-danger&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("发布失败，点击返回新闻稿发布页") + "&preview=edit.jsp");
            }
        } else {
            toPath += ("state=panel-danger&title=" + URLEncoder.encode("新闻稿") + "&context=" + URLEncoder.encode("发布失败，新闻稿信息不完全") + "&preview=edit.jsp");
        }
        boolean isManager = request.getSession().getAttribute("isManager") == null ? false : (boolean) request.getSession().getAttribute("isManager");
        historyServer.record(DealWithAddress.dealWith(request.getRemoteAddr()), ua, Application.COMPUTER_BASE_NAME[1], isManager ? "普通用户" : "系统管理员", (String) request.getSession().getAttribute("user"), "提交新闻稿", flag ? "成功" : "失败");
        response.sendRedirect(toPath);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
